
package com.rs.service.impl.rs;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rs.dao.DocumentMapper;
import com.rs.model.CommonResult;
import com.rs.model.User;
import com.rs.model.Document;
import com.rs.model.Project;
import com.rs.model.Relation;
import com.rs.service.rs.DocumentService;
import com.rs.util.CheckFileTypeUtil;
import com.rs.util.DocumentConversionUtil;
import com.rs.util.FileOperationsUtil;
import com.rs.util.TimeTypeConversionUtil;

@Service
public class DocumentServiceImpl implements DocumentService {

	@Autowired
	private DocumentMapper dm;

	/*********APP************/

	/**
	 * 文档上传
	 */
	@Override
	@Transactional
	public CommonResult<String> documentUploadApp(Document document) {
		//判断上传人是否为该项目参与者
		//根据上传人id和项目id
		Relation relation = dm.judgeUpUserRelation(document.getDo_uid(), document.getDo_prid());
		boolean flag = Optional.ofNullable(relation).isPresent();
		if (flag) {//上传人为该项目参与者
			//判断上传的文件名是否重复
			Document doc = dm.selectDocByDoname(document.getDo_name());
			boolean f = Optional.ofNullable(doc).isPresent();
			if (!f) {//没有重复
				String base64 = document.getDo_base64();
				String rootPath = "C://CompayRs//document//" + document.getDo_prname();
				String doname = document.getDo_name();//文件名
				String fileType = doname.substring(doname.lastIndexOf("."), doname.length());//文件后缀
				//文件路径
				String path = FileOperationsUtil.getFilePath(base64, rootPath, fileType);
				//将文件路径赋值给document对象
				document.setDo_url(path);
				document.setDo_time(new Date());//设置上传时间
				int count = dm.documentUploadApp(document);
				if (count != 0) {
					return new CommonResult<String>(200, "上传成功");
				} else {
					return new CommonResult<String>(200, "上传失败");
				}
			} else {
				return new CommonResult<String>(200, "文件名重复，请重新上传");
			}

		} else {
			return new CommonResult<String>(200, "上传人只能为项目组成员");
		}
	}

	/**
	 * 查看用户上传的所有文档
	 */
	@Override
	public CommonResult<List<Document>> documentViewUserUpFileApp(User user) {
		List<Document> list = dm.documentViewUserUpFileApp(user.getU_id());
		return new CommonResult<List<Document>>(200, "查询成功", list);
	}

	/**
	 * 查询所有的项目名称
	 */
	@Override
	public CommonResult<List<Project>> doucumenLookAllProApp() {
		List<Project> list = dm.doucumenLookAllProApp();
		return new CommonResult<List<Project>>(200, "查询成功", list);
	}

	/**
	 * 查询项目下的所有文档
	 */
	@Override
	public CommonResult<List<Document>> doucumenLookProAllDouApp(Project project) {
		//判断查询人是否为该项目组的成员
		//传pr_upuid,pr_id
		Integer pr_id = project.getPr_id();
		Relation relation = dm.judgeUpUserRelation(project.getPr_upuid(), pr_id);
		boolean flag = Optional.ofNullable(relation).isPresent();
		if (flag) {//查询人为该项目组成员
			List<Document> list = dm.doucumenLookProAllDouApp(pr_id);
			return new CommonResult<List<Document>>(200, "查询成功", list);
		} else {
			return new CommonResult<List<Document>>(200, "查询项目文档只能为项目组成员");
		}

	}

	/**
	 * 查看文档详情
	 */
	@Override
	public CommonResult<Document> doucumenLookDetailsApp(Integer do_id) {
		Document document = dm.selectDoucumenByDoid(do_id);
		return new CommonResult<Document>(200, "查询成功", document);
	}

	/**
	 * 文档预览
	 */
	@Override
	public CommonResult<String> doucumenPreviewApp(Integer do_id) {
		//通过do_id查询doucumen
		Document document = dm.selectDoucumenByDoid(do_id);
		boolean flag = Optional.ofNullable(document).isPresent();
		if (!flag) {//document为空
			return new CommonResult<String>(200, "未查到该文档");
		}

		String doname = document.getDo_name();//文件名
		String suffix = CheckFileTypeUtil.getFileType(document.getDo_url());//文件后缀
		String filePath = null;//返回的文件路径
		String msg = "查询成功";//返回的消息
		switch (suffix) {
		case "xls"://03的文件
			if (doname.indexOf("doc") != -1) {//xls文件
				filePath = DocumentConversionUtil.wordConversion(document.getDo_prname(), document.getDo_url());
			}
			if (doname.indexOf("xls") != -1) {//doc
				filePath = DocumentConversionUtil.excelConversion(document.getDo_prname(), document.getDo_url());
			}
			break;
		case "xlsx"://07的文件
			if (doname.indexOf("docx") != -1) {//xls文件
				filePath = DocumentConversionUtil.wordConversion(document.getDo_prname(), document.getDo_url());
			}
			if (doname.indexOf("xls") != -1) {//doc
				filePath = DocumentConversionUtil.excelConversion(document.getDo_prname(), document.getDo_url());
			}
			break;
		case "jpg":
			filePath = document.getDo_url();
			break;
		case "png":
			filePath = document.getDo_url();
			break;
		default:
			msg = "暂不支持该类型的文件预览";
			break;
		}

		return new CommonResult<String>(200, msg, filePath);
	}

	/**
	 * 文档下载
	 */
	@Override
	public CommonResult<String> doucumenDownloadApp(Integer do_id) {
		//通过do_id查询doucumen
		Document document = dm.selectDoucumenByDoid(do_id);
		String base64 = FileOperationsUtil.getFileBase64(document.getDo_url());
		return new CommonResult<String>(200, "下载成功", base64);
	}

	/**
	 * 文档删除
	 */
	@Override
	@Transactional
	public CommonResult<String> doucumenDeleteOneApp(Map<String, Integer> map) {
		//判断删除人是否为文档创建人
		Document d = dm.selectDocumentByDoidUid(map);
		boolean flag = Optional.ofNullable(d).isPresent();
		if (!flag) {//未通过删除人的id查到文档
			return new CommonResult<String>(200, "文档只能由创建人删除");
		}
		int count = dm.deleteByPrimaryKey(map.get("do_id"));
		if (count != 0) {
			return new CommonResult<String>(200, "删除成功");
		} else {
			return new CommonResult<String>(200, "删除失败");
		}

	}

	/***********PC******/
	/**
	 * 查询所有的文档记录PC
	 */
	@Override
	public CommonResult<Map<String, Object>> doucumenLookAllPc(Integer pageNum) {
		Page<Document> page = PageHelper.startPage(pageNum, 10);
		List<Document> list = dm.doucumenLookAllPc();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("total", page.getTotal());
		return new CommonResult<Map<String, Object>>(200, "查询成功", map);
	}

	/**
	 * pc端条件查询
	 */
	@Override
	public CommonResult<Map<String, Object>> doucumenConditionsPc(Document document) {
		Page<Document> page = PageHelper.startPage(document.getPageNum(), 10);
		List<Document> list = dm.doucumenConditionsPc(document);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("total", page.getTotal());
		map.put("pageNum", page.getPageNum());
		map.put("pages", page.getPages());
		return new CommonResult<Map<String, Object>>(200, "查询成功", map);
	}

	/**
	 * pc端依据上传时间查询
	 */
	@Override
	public CommonResult<Map<String, Object>> documenUploadTimeQueryPc(Map<String, String> map) {
		//开始时间
		Date startTime = TimeTypeConversionUtil.getDateByString(map.get("startTime"));
		//结束时间
		Date endTime = TimeTypeConversionUtil.getDateByString(map.get("endTime"));

		Page<Document> page = PageHelper.startPage(Integer.parseInt(map.get("pageNum")), 10);
		List<Document> list = dm.documenUploadTimeQueryPc(startTime, endTime);

		Map<String, Object> mm = new HashMap<String, Object>();
		mm.put("list", list);
		mm.put("total", page.getTotal());
		return new CommonResult<Map<String, Object>>(200, "查询成功", mm);

	}

	/**
	 * pc端查询文档的详情
	 */
	@Override
	public CommonResult<Document> documenForDetailsPc(Integer do_id) {
		Document dd = dm.selectByPrimaryKey(do_id);
		return new CommonResult<Document>(200, "查询成功", dd);
	}

	/**
	 * pc端下载
	 */
	@Override
	public CommonResult<Document> documenDownloadPc(Integer do_id) {
		Document document = dm.selectByPrimaryKey(do_id);
		String base64 = FileOperationsUtil.getFileBase64(document.getDo_url());
		document.setDo_base64(base64);
		return new CommonResult<Document>(200, "下载成功", document);
	}

}
